package com.autoEcole.service;

import java.util.List;
import java.util.Map;

import com.autoEcole.dao.CoursDAO;
import com.autoEcole.dao.EleveDAO;
import com.autoEcole.dao.EleveExamenDAO;
import com.autoEcole.dao.ExamenDAO;
import com.autoEcole.dao.PermisDAO;
import com.autoEcole.dao.ResultatDAO;
import com.autoEcole.exception.ApplicationException;
import com.autoEcole.exception.DAOException;
import com.autoEcole.model.Cours;
import com.autoEcole.model.Eleve;
import com.autoEcole.model.EleveExamen;
import com.autoEcole.model.Examen;
import com.autoEcole.model.Permis;
import com.autoEcole.model.Resultat;

public class EleveExamenService {
	private EleveExamenDAO eleveExamenDAO;
	private CoursDAO coursDAO;
	private EleveDAO eleveDAO;
	private ExamenDAO examenDAO;
	private PermisDAO permisDAO;
	private ResultatDAO resultatDAO;
	/**
	 * EleveExamenService
	 */
	private static EleveExamenService eleveExamenService;
	
	/**
	 * Constructor
	 */
	private EleveExamenService() {
		super();
	}

	/**
	 * Returns the EleveExamenService instance (Design Pattern : Singleton)
	 * 
	 * @return EleveExamenService
	 */
	public static synchronized EleveExamenService getInstance() {
		if (eleveExamenService == null)
			eleveExamenService = new EleveExamenService();
		return eleveExamenService;
	}

	public EleveExamenDAO getEleveExamenDAO() {
		return eleveExamenDAO;
	}

	public void setEleveExamenDAO(EleveExamenDAO eleveExamenDAO) {
		this.eleveExamenDAO = eleveExamenDAO;
	}

	public CoursDAO getCoursDAO() {
		return coursDAO;
	}

	public void setCoursDAO(CoursDAO coursDAO) {
		this.coursDAO = coursDAO;
	}

	public EleveDAO getEleveDAO() {
		return eleveDAO;
	}

	public ExamenDAO getExamenDAO() {
		return examenDAO;
	}

	public void setExamenDAO(ExamenDAO examenDAO) {
		this.examenDAO = examenDAO;
	}

	public void setEleveDAO(EleveDAO eleveDAO) {
		this.eleveDAO = eleveDAO;
	}

	public PermisDAO getPermisDAO() {
		return permisDAO;
	}

	public void setPermisDAO(PermisDAO permisDAO) {
		this.permisDAO = permisDAO;
	}

	public ResultatDAO getResultatDAO() {
		return resultatDAO;
	}

	public void setResultatDAO(ResultatDAO resultatDAO) {
		this.resultatDAO = resultatDAO;
	}

	public void create(EleveExamen eleveExamen) throws ApplicationException {
		try {
			eleveExamenDAO.create(eleveExamen);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}

	/**
	 * 
	 */
	public void update(EleveExamen eleveExamen) throws ApplicationException {
		try {
			eleveExamenDAO.update(eleveExamen);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}

	/**
	 * retourne l'element si il existe ou null
	 */
	public List<EleveExamen> findByEleveId(int eleveId) throws ApplicationException {
		try {
			Map<Integer, Cours> mapCours = coursDAO.findAllMap();
			Map<Integer, Eleve> mapEleve = eleveDAO.findAllMap();
			Map<Integer, Examen> mapExamen = examenDAO.findAllMap();
			Map<Integer, Permis> mapPermis = permisDAO.findAllMap();
			Map<Integer, Resultat> mapResultat = resultatDAO.findAllMap();
			List<EleveExamen> listEleveExamen = eleveExamenDAO.findByEleveId(eleveId);
			for (EleveExamen eleveExamen : listEleveExamen) {
				eleveExamen.setEleve(mapEleve.get(eleveId));
				int coursId = eleveExamen.getCours().getCoursId();
				eleveExamen.setCours(mapCours.get(coursId));
				int examenId = eleveExamen.getExamen().getExamenId();
				eleveExamen.setExamen(mapExamen.get(examenId));
				int permisId = eleveExamen.getPermis().getPermisId();
				eleveExamen.setPermis(mapPermis.get(permisId));
				int resultatId = eleveExamen.getResultat().getResultatId();
				eleveExamen.setResultat(mapResultat.get(resultatId));
			}
			return listEleveExamen;
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}
	
	/**
	 * 
	 */	
	public void delete(EleveExamen eleveExamen) throws ApplicationException {
		try {
			eleveExamenDAO.delete(eleveExamen);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}		
	}
}
